package com.example.daxiguaforharmony;

import com.example.daxiguaforharmony.utils.LogUtil;
import com.example.daxiguaforharmony.manager.CrossDevicePlayerInterfaceStub;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Environment;
import ohos.media.common.Source;
import ohos.media.photokit.metadata.AVMetadataHelper;
import ohos.media.player.Player;
import ohos.rpc.IRemoteObject;
import ohos.rpc.RemoteException;

import java.io.*;

public class PlayAbility  extends Ability {
    public static final int PLAY_MUSIC_MSG = 100;
    public static final int PAUSE_MUSIC_MSG=101;
    public static final int STOP_MUSIC_MSG=102;
    public static final int CONTINUE_MUSIC_MSG=103;
    public static final int KEY_VOLUME_UP=112;
    public static final int KEY_VOLUME_DOWM=113;

    public static final int CHANGE_PROGRESS=201;
    public static final int TOTAL_PROGRESS=202;
    public static final int UPDATE_PROGRESS=203;

    public static final int CURRENT_PLAY_TIME=301;
    private static Source sVideoSource;
    private boolean isPlay=false;
    private static AVMetadataHelper sAvMetadataHelper = new AVMetadataHelper();

    public Player sPlayer;
    @Override
    protected void onStart(Intent intent) {
        File mp3FilePath = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
        File mp3File = new File(mp3FilePath.getAbsolutePath());
    }

    @Override
    protected void onActive() {
        super.onActive();

    }

    @Override
    protected void onStop() {
        super.onStop();
        cancelBackgroundRunning();
    }
    @Override
    protected void onDisconnect(Intent intent) {
        super.onDisconnect(intent);
    }

    @Override
    protected IRemoteObject onConnect(Intent intent) {
        super.onConnect(intent);
        return new MusicRemountObject("MusicRemountObject").asObject();
    }

    private class MusicRemountObject extends CrossDevicePlayerInterfaceStub {
        public MusicRemountObject(String descriptor) {
            super(descriptor);
        }

        @Override
        public void sendCommand(int command, long value) throws RemoteException {
            switch (command) {
                case PLAY_MUSIC_MSG:
                    System.out.println("收到命令======");
                    preparePlayer();
                    if(sPlayer.isNowPlaying()){
                        break;
                    }
                    if(isPlay){
                        sPlayer.play();
                        break;
                    }
                    playMP3File("test.mp3");
                    getProgress();
                    break;
                case STOP_MUSIC_MSG:
                    preparePlayer();
                    sPlayer.stop();
                    isPlay=false;
                    break;
                case PAUSE_MUSIC_MSG:
                    preparePlayer();
                    sPlayer.pause();
                    break;
                case CONTINUE_MUSIC_MSG:
                    preparePlayer();
                    sPlayer.play();
                    break;
//                case KEY_VOLUME_UP:
//                    audio.vloumnUp();
//                    break;
//                case KEY_VOLUME_DOWM:
//                    audio.vloumnDown();
//                    break;
//                case CHANGE_PROGRESS:
//                    MainAbilitySlice.getHandler().sendEvent((int)value);
//                    break;
                case UPDATE_PROGRESS:
                    sPlayer.rewindTo(value*1000);
                    break;
                default:
                    break;
            }
        }
    };


    public boolean preparePlayer(){
        if(sPlayer==null){
            sPlayer = new Player(PlayAbility.this);
        }
        return true;
    }

    private void playMP3File(String mediaName) {
        preparePlayer();
        File mp3FilePath = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
        LogUtil.info("PlayAbility", "私有MP3路径: " + mp3FilePath.getAbsolutePath());
        File mp3File = new File(mp3FilePath.getAbsolutePath() + "/"+mediaName);
        try {
            FileInputStream fileInputStream = new FileInputStream(mp3File);
            FileDescriptor fileDescriptor = fileInputStream.getFD();
            sVideoSource = new Source(fileDescriptor);
            sPlayer.setSource(sVideoSource);
            //sPlayer.setPlayerCallback(mPlayerCallback);
            sPlayer.prepare();
            isPlay=true;
            updateProgress();
            sAvMetadataHelper.setSource(fileDescriptor);
        } catch (FileNotFoundException e) {
            LogUtil.info("PlayAbility", e.getMessage());
        } catch (IOException e) {
            LogUtil.info("PlayAbility", e.getMessage());
        }
    }

    public void getProgress(){
        int duration = sPlayer.getDuration();
        //MainAbilitySlice.getHandler().sendEvent(InnerEvent.get(TOTAL_PROGRESS,duration));
    }

    public void updateProgress(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(isPlay){
                    try {
                        new Thread().sleep(500);
                        int currentTime = sPlayer.getCurrentTime();
                        //MainAbilitySlice.getHandler().sendEvent(InnerEvent.get(CHANGE_PROGRESS,currentTime));

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
